<?php

namespace Wilead\ApiBundle\Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of JobCollection
 *
 * @author jack
 */
class JobCollectionController extends ApiController {

    /**
     * @ApiDoc(
     *      description="用户订阅的职位"
     * )
     */
    public function getAction($id) {
        $qb = $this->getRepository('WileadIArtistBundle:JobCollection')->createQueryBuilder('q');

        $qb
                ->select('q, u, j, ju, jui, jur')
                ->leftJoin('q.Users', 'u')
                ->leftJoin('q.Jobs', 'j')
                ->leftJoin('j.User', 'ju')
                ->leftJoin('ju.UserIntroduction', 'jui')
                ->leftJoin('ju.UserResume', 'jur')
                ->where('u.id = :uid')->setParameter('uid', $id)
                ->orderBy('q.id', 'DESC');

        return $this->getFOSView()->setStatusCode(200)->setData($qb->getQuery()->getArrayResult());
    }

    /**
     * @ApiDoc(
     *      description="职位订阅"
     * )
     * @RequestParam(name="uid", requirements="integer", description="用户ID")
     * @RequestParam(name="jid", requirements="integer", description="职位ID")
     */
    public function addAction() {
        $em = $this->getEntityManager();
        $request = $this->getRequest()->request;
        $collection = new \Wilead\IArtistBundle\Entity\JobCollection;
        $collection->setUsers($em->getReference('Wilead\IArtistBundle\Entity\User', $request->get('uid')));
        $collection->setJobs($em->getReference('Wilead\IArtistBundle\Entity\Job', $request->get('jid')));
        $em->persist($collection);
        $em->flush($collection);

        return $this->getFOSView()->setData('收藏成功')->setStatusCode(200);
    }

    /**
     * @ApiDoc(
     *      description="删除职位订阅"
     * )
     * @RequestParam(name="id", requirements="integer", description="职位订阅ID")
     */
    public function deleteAction() {
        $em = $this->getEntityManager();
        $request = $this->getRequest()->request;
        $collection = $this->getRepository('WileadIArtistBundle:JobCollection')->find($request->get('id'));

        if (!$collection)
            return $this->getFOSView()->setStatusCode(404);

        $em->remove($collection);
        $em->flush();

        return $this->getFOSView()->setStatusCode(200)->setData('职位订阅删除成功');
    }

}

?>
